import flash.display.BitmapData;

import pl.milib.tools.LoadingMC;
import pl.milib.util.MIBitmapUtil;
import pl.milib.util.MILibUtil;

/**
 * @author Marek Brun
 */
class pl.milib.tools.CachedLoadedImage extends LoadingMC {
	
	static private var INSTANCES_BY_URL : Object = {};	static private var COUNT : Object = {num:0};

	private var holder : MovieClip;
	private var bd : BitmapData;
	
	private function CachedLoadedImage(url:String, holder:MovieClip) {
		super(holder, url);
		this.holder=holder;
		start();
	}//<>
	
	public function getBitmapData(Void):BitmapData {
		if(!bd){
			bd=MIBitmapUtil.getBitmapFromMC(holder);
		}
		return bd;
	}//<<
	
	static public function getInstanceByURL(url:String):CachedLoadedImage {
		if(!INSTANCES_BY_URL[url]){
			COUNT.num++;
			if(!MILibUtil.getMCMILibMC(_root).cachedLoadedImages){
				var cachedLoadedImages:MovieClip=MILibUtil.getMCMILibMC(_root).createEmptyMovieClip('cachedLoadedImages', MILibUtil.getMCMILibMC(_root).getNextHighestDepth());
				cachedLoadedImages._x=cachedLoadedImages._y=10000;
				cachedLoadedImages._width=cachedLoadedImages._height=1;
				cachedLoadedImages._visible=false;
			}
			var holder:MovieClip=MILibUtil.getMCMILibMC(_root).cachedLoadedImages.createEmptyMovieClip('holder'+COUNT.num, MILibUtil.getMCMILibMC(_root).cachedLoadedImages.getNextHighestDepth());
			INSTANCES_BY_URL[url]=new CachedLoadedImage(url, holder);
		}
		return INSTANCES_BY_URL[url];
	}//<<
	
}